package com.hightemperator.service;

import com.hightemperator.dao.*;
import com.hightemperator.po.*;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private SysUserMapper sysuserMapper;
    @Resource
    private SysUserAttentionMapper sysUserAttentionMapper;
    @Resource
    private BlacklistMapper blacklistMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private SysPayRecordMapper sysPayRecordMapper;
    @Autowired
    private AuthenticationCarMapper authenticationCarMapper;
    @Autowired
    private AuthenticationEducationMapper authenticationEducationMapper;
    @Autowired
    private AuthenticationHeadimageMapper authenticationHeadimageMapper;
    @Autowired
    private AuthenticationHouseMapper authenticationHouseMapper;
    @Autowired
    private AuthenticationJobMapper authenticationJobMapper;
    @Autowired
    private CircleMapper circleMapper;
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 添加用户
     *
     * @param sysuser
     * @return
     */
    public int addUser(SysUser sysuser,String type) {
        if (type.equals("1")){
            SysUserExample example = new SysUserExample();
            SysUserExample.Criteria criteria = example.createCriteria();
            criteria.andUsermobileEqualTo(sysuser.getUsermobile());
            return sysuserMapper.updateByExampleSelective(sysuser,example);
        }
        return sysuserMapper.insertSelective(sysuser);
    }

    /**
     * 通过电话找到用户(并返回认证信息)
     *
     * @param phone
     * @return
     */
    public List<SysUserAndAuthentication> getByMobile(String phone) {
        return sysuserMapper.selectUserAllMessageByMobile(phone);
    }

    /**
     * 获取用户包括认证信息的全部信息
     *
     * @param uId
     * @param wxId
     * @param loginUserId
     * @return
     */
    public List<SysUserAndAuthentication> getUserAllMessage(Integer uId, String wxId, Integer loginUserId) {
        return sysuserMapper.selectUserAllMessage(uId, loginUserId);
    }

    public SysUser getUserMessage(Integer uId){
        return sysuserMapper.selectByPrimaryKey(uId);
    }


    /**
     * 通过id或微信id找到用户
     *
     * @param uId
     * @param wxId
     * @return
     */
    public List<SysUser> getUserById(Integer uId, String wxId) {
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        if (uId != null && !"".equals(uId)) {
            criteria.andUidEqualTo(uId);
        }
        //为微信登录 暂留
//        if (wxId != null && !"".equals(wxId)) {
//            criteria.andWxIdEqualTo(wxId);
//        }
        return sysuserMapper.selectByExample(example);
    }


    /**
     * 修改用户
     *
     * @param sysUser
     * @return
     */
    public int updateUser(SysUser sysUser) {
        return sysuserMapper.updateByPrimaryKeySelective(sysUser);
    }

    /**
     * 根据用户id 查询我关注的列表
     *
     * @param uId
     * @return
     */
    public List<SysUserAttention> getAttentionList(Integer uId) {
        SysUserAttentionExample example = new SysUserAttentionExample();
        SysUserAttentionExample.Criteria criteria = example.createCriteria();
        criteria.andUseridEqualTo(uId);
        return sysUserAttentionMapper.selectByExample(example);
    }

    /**
     * 根据用户id 获取 被关注列表
     *
     * @param uId
     * @return
     */
    public List<SysUserAttention> getBeAttentionList(Integer uId) {
        SysUserAttentionExample example = new SysUserAttentionExample();
        SysUserAttentionExample.Criteria criteria = example.createCriteria();
        criteria.andAttentionidEqualTo(uId);
        return sysUserAttentionMapper.selectByExample(example);
    }

    /**
     * 根据用户id 获取 相互关注列表
     *
     * @param uId
     * @return
     */
    public List<SysUserAttention> getEachOtherAttentionList(Integer uId) {
        return sysUserAttentionMapper.getEachOtherAttention(uId);
    }

    /**
     * 根据用户id获取黑名单列表
     *
     * @return
     */
    public List<Blacklist> getBlackListMessage(Integer uId) {
        return blacklistMapper.getBlackListMessage(uId);
    }

    /**
     * 添加关注
     *
     * @param sysUserAttention
     * @return
     */
    public int addAttention(SysUserAttention sysUserAttention) {
        return sysUserAttentionMapper.insertSelective(sysUserAttention);
    }

    /**
     * 添加黑名单
     *
     * @param blacklist
     * @return
     */
    public int addBlackList(Blacklist blacklist) {
        return blacklistMapper.insertSelective(blacklist);
    }

    /**
     * 添加举报
     *
     * @param report
     * @return
     */
    public int addReport(Report report) {
        return reportMapper.insertSelective(report);
    }


    /**
     * 根据id获取举报列表
     *
     * @param uId
     * @return
     */
    public List<Report> getReportList(Integer uId) {
        ReportExample example = new ReportExample();
        ReportExample.Criteria criteria = example.createCriteria();
        criteria.andCreateuseridEqualTo(uId);
        return reportMapper.selectByExample(example);
    }

    /**
     * 根据用户id获取充值记录列表
     *
     * @param uId
     * @return
     */
    public List<SysPayRecord> getPayRecord(Integer uId) {
        SysPayRecordExample example = new SysPayRecordExample();
        SysPayRecordExample.Criteria criteria = example.createCriteria();
        criteria.andCreatoridEqualTo(uId);
        return sysPayRecordMapper.selectByExample(example);
    }

    /**
     * 操作用户解除或进入免打扰模式
     *
     * @param uId
     * @return
     */
    public int updateNoDisturbing(Integer uId) {
        SysUser sysUser = sysuserMapper.selectByPrimaryKey(uId);

        String type = (sysUser.getNodisturbing() != null ? sysUser.getNodisturbing() : "0").equals("1") ? "0" : "1";
        System.out.println(type);
        int successCode = sysuserMapper.updateNoDisturbing(uId, type);
        return successCode;
    }

    /**
     * 根据id 获取用户免打扰状态（0为正常模式，1为免打扰模式）
     *
     * @param uId
     * @return
     */
    public String getUserNoDisturbing(Integer uId) {
        String str = sysuserMapper.getUserNoDisturbing(uId);
        return str;
    }

    /**
     * 添加车辆认证
     * @param path
     * @param uId
     * @return
     */
    public int addAuthenticationCar(String path,Integer uId){
        AuthenticationCar authenticationCar = new AuthenticationCar();
        authenticationCar.setUpdateimage(path);
        authenticationCar.setCorrelationid(uId);
        return authenticationCarMapper.insertSelective(authenticationCar);
    }

    /**
     * 添加学历认证
     * @param path
     * @param uId
     * @return
     */
    public int addAuthenticationEducation(String path,Integer uId){
        AuthenticationEducation authenticationEducation = new AuthenticationEducation();
        authenticationEducation.setUpdateimage(path);
        authenticationEducation.setCorrelationid(uId);
        return authenticationEducationMapper.insertSelective(authenticationEducation);
    }

    /**
     * 添加头像认证
     * @param path
     * @param uId
     * @return
     */
    public int addAuthenticationHeadimage(String path,Integer uId){
        AuthenticationHeadimage authenticationHeadimage = new AuthenticationHeadimage();
        authenticationHeadimage.setUpdateimage(path);
        authenticationHeadimage.setCorrelationid(uId);
        return authenticationHeadimageMapper.insertSelective(authenticationHeadimage);
    }

    /**
     * 添加房产认证
     * @param path
     * @param uId
     * @return
     */
    public int addAuthenticationHouse(String path,Integer uId){
        AuthenticationHouse authenticationHouse = new AuthenticationHouse();
        authenticationHouse.setUpdateimage(path);
        authenticationHouse.setCorrelationid(uId);
        return authenticationHouseMapper.insertSelective(authenticationHouse);
    }

    /**
     * 添加工作认证
     * @param path
     * @param uId
     * @return
     */
    public int addAuthenticationJob(String path,Integer uId){
        AuthenticationJob authenticationJob = new AuthenticationJob();
        authenticationJob.setUpdateimage(path);
        authenticationJob.setCorrelationid(uId);
        return authenticationJobMapper.insertSelective(authenticationJob);
    }

    public int deleteCircleOrActivity(Integer id,String type){
        int successCode;
        if (type.equals("0")){
            successCode = activityMapper.deleteByPrimaryKey(id);
        }else if(type.equals("1")){
            successCode = circleMapper.deleteByPrimaryKey(id);
        }else {
            return 0;
        }
        return successCode;
    }


}